package com.bazaaroid.client.web.gwt.partnermodule.client.view.partnerinfo;

import com.bazaaroid.client.web.gwt.partnermodule.client.dto.PartnerInfoDTO;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.common.TitleCommandBar;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.partnerinfo.impl.PartnerInfoTabViewImpl;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class EditPartnerInfoEntryView extends EditPartnerGenericEntryView<PartnerInfoDTO> {

	private PartnerInfoEntryView partnerInfoEntryView = (PartnerInfoEntryView) getPartnerGenericEntryView();
	private TitleCommandBar titleBar = new TitleCommandBar("New PartnerInfo");
	private TextBox nameTextBox = new TextBox();
	private RichTextArea descriptionTextArea = new RichTextArea();
	
	private class SaveClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			getPartnerGenericDTO().setName(nameTextBox.getText());
			getPartnerGenericDTO().setDescription(descriptionTextArea.getHTML());
			getPartnerInfoTabView().onPartnerInfoEntrySaved(partnerInfoEntryView, EditPartnerInfoEntryView.this, getPartnerGenericDTO());
		}
	}
	
	private class CancelClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			if(partnerInfoEntryView != null) 
				partnerInfoEntryView.getPartnerInfoTabView().endEditPartnerInfo(EditPartnerInfoEntryView.this, partnerInfoEntryView);
			else 
				removeFromParent();
		}
	}
	
	public EditPartnerInfoEntryView(PartnerInfoTabViewImpl partnerInfosTabView, PartnerInfoDTO partnerInfoDTO) {
		super(partnerInfosTabView, partnerInfoDTO);
		
		VerticalPanel editPartnerInfoPanel = new VerticalPanel();
		initWidget(editPartnerInfoPanel);
		
		titleBar.addCommand("Save", new SaveClickHandler());
		titleBar.addCommand("Cancel", new CancelClickHandler());
		editPartnerInfoPanel.add(titleBar);
		
		editPartnerInfoPanel.add(new Label("Name"));
		editPartnerInfoPanel.add(nameTextBox);
		editPartnerInfoPanel.add(new Label("Description"));
		editPartnerInfoPanel.add(descriptionTextArea);
		editPartnerInfoPanel.setStyleName("partnerInfoPanel");
	}
	
	public EditPartnerInfoEntryView(PartnerInfoEntryView partnerInfoEntryView) {
		this(partnerInfoEntryView.getPartnerInfoTabView(), partnerInfoEntryView.getPartnerGenericDTO());
		this.partnerInfoEntryView = partnerInfoEntryView;
		
		titleBar.setTitle("Edit PartnerInfo");
		nameTextBox.setText(getPartnerGenericDTO().getName());
		descriptionTextArea.setHTML(getPartnerGenericDTO().getDescription());
		
	}

}
